#include "UART2.h"

void USART2_Init(void){
    // 开启时钟（USART2在APB1总线）
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    
    // 初始化引脚（PA2-TX，PA3-RX）
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;     // PA2:USART2_TX
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;     // PA3:USART2_RX
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    // USART配置
    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_Init(USART2, &USART_InitStructure);    
    
    // 中断配置
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 12;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    // 启动USART2
    USART_Cmd(USART2, ENABLE);
}

// 中断处理函数
//void USART2_IRQHandler(void) {
//    if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {
//        char data = USART_ReceiveData(USART2);
//        BaseType_t stat = pdTRUE;
//        xQueueSendFromISR(queue, &data, &stat);
//        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
//    }
//}

// 串口1的中断处理函数
void USART2_IRQHandler(void) {
	if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {
			
		// 读取一个字节
		 char data = USART_ReceiveData(USART2);

		// 存储到数组
		buf_usart2[strlen(buf_usart2)] = data;
				
		 USART_ClearITPendingBit(USART2, USART_IT_RXNE);
	}
}


// 发送单字节
void USART2_SendByte(uint8_t Byte){
    USART_SendData(USART2, Byte);
    while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}

// 格式化输出函数
void printf2(char *format, ...){
    char strs[100] = {0};    
    va_list list;
    va_start(list, format);
    vsprintf(strs, format, list);
    va_end(list);
    for(uint8_t i=0; strs[i] != '\0'; i++){
        USART2_SendByte(strs[i]);
    }
}
